<?php
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */

namespace App\Controller\Api;


use App\Service\WechatService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

/**
 * @Controller(prefix="notify")
 */
class PayNotifyController
{
    /**
     * @Inject()
     * @var WechatService
     */
    protected $wechatService;

    /**
     * @GetMapping(path="wx")
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxNotify()
    {
        //商户回调通知是<xml>格式
        $param = file_get_contents("php://input");
        //var_dump(for_xml_to_json($param));
        if (!empty($param)) {
            $param = json_decode(for_xml_to_json($param), true);

            $outTradeNo = $param['out_trade_no'];
            $attach = !empty($param['attach']) ? json_decode($param['attach'], true) : '';
            if ($outTradeNo) {//通知中是否包含订单号 out_trade_no

                //查询商户平台该订单状态
                $bill = $this->wechatService->queryBill($outTradeNo);
                if ($bill['return_code'] == 'SUCCESS' && $bill['result_code'] == 'SUCCESS') {
                    switch ($attach['module']) {
                        case 'goods_order' :
                            //查询后台订单为  待支付状态的订单
                            $order = [];
                            if (!$order) {
                                json(['code' => -1, 'msg' => '该订单不存在'])->send();
                                exit;
                            } else {
                                //查询账单状态并更新
                                $where['id'] = $order['id'];
                                $data['status'] = 2;//2为已支付
                                $data['transaction_id'] = $param['transaction_id'];
                                $data['pay_time'] = date("Y-m-d H:i:s", time());
                                $res = $order->updateOne($data, $where);

                                if ($res) {
                                    //其他通知 TODO
                                    $backInfo['return_code'] = 'SUCCESS';
                                    $backInfo['return_msg'] = '校验成功';
                                    return for_array_to_xml($backInfo);
                                } else {
                                    $backInfo['return_code'] = 'FAIL';
                                    $backInfo['return_msg'] = '校验失败';
                                    return for_array_to_xml($backInfo);
                                }
                            }
                            break;
                       default:
                            break;
                    }

                } else {
                    json(['code' => -1, 'msg' => '异常请求'])->send();
                    exit;
                }
            } else {
                json(['code' => -1, 'msg' => '异常请求'])->send();
                exit;
            }
        } else {
            json(['code' => -1, 'msg' => '异常请求'])->send();
            exit;
        }
    }
}